<?php


	namespace app\api\controller;


	use app\admin\model\order\OrderModel;
	use core\basic\Controller;


	class OrderController extends Controller
	{
		protected $model;

		public function __construct()
		{


			$this->model = new OrderModel();

		}

		public function orderInfo()
		{
			$this->Check('', '../index.html');
			$ooder = $this->model->checkOrder();
			$number = explode(",", $ooder->amount);
			$data = array();
			$i = 0;
			foreach ($number as $k => $v) {
				$data[$i]['id'] = $k + 1;
				$data[$i]['value'] = $v;
				$i++;
			}
			$unit_price = explode(",", $ooder->unit_price);

			$priceArr = array();
			$i1 = 0;
			foreach ($unit_price as $key => $value) {
				$priceArr[$i1]['id'] = $key + 1;
				$priceArr[$i1]['value'] = $value;
				$i1++;
			}

			$dataInfo = array(
				'info' => $ooder,
				'data' => $data,
				'price' => $priceArr
			);
			json(1, $dataInfo);
		}

		/**
		 *status 1发布中  2 交易中  3 交易完成 4 交易失败
		 * sellType 1 卖出 2 买入
		 */
		public function add_order()
		{

			$this->Check('', '../index.html');
			$userInfo = model('admin.member.Member')->getMember($this->Memcache(2, session('uid'))->id);//当前会员信息

			$number = request('number', 'null', true, '求购数量');//求购数量

			$unit_price = request('unit_price', 'num', true, '单价');//单价
			$TradersPassword = request('TradersPassword', 'null', true, '交易密码');//单价
			$patternId = request('patternId', 'num', true, '付款类型');//收款类型
			$sellType = request('sellType', 'num', true, '订单类型');//收款类型


//			if (md5(md5($TradersPassword)) != $userInfo->TradersPassword) {
//				json(0, '交易密码错误!');
//			}
			if ($patternId == 0) {
				$bankId = request('bankId', 'num', true, '银行卡');//银行卡

			} else {

				$alipay = request('alipay', 'null', true, '支付宝账号');//支付宝账号
			}
			//判断是否在订单池抢过奖励


			$ooder = $this->model->checkOrder();//排单信息
			if ($ooder->status != 2) {
				json(0, '挂卖时间未开启!');
			}

			$sxf = $ooder->poundage; //手续费
			if ($sxf != 0.00) {
				$total = $number - ($number * $unit_price) * $sxf; //实际扣除手续费金额
			} else {
				$total = $number * $unit_price; // 实际不扣除手机费金额
			}
			$ooderInfo = $this->model->userOrdercheck(['user_id' => $userInfo->id]);

			if ($sellType == 1) {
				if ($userInfo->up_integral < $total) {
					json(0, '积分不足,无法出售！');
				}
				if ($ooderInfo->sellType == 1 && $ooderInfo->status == 1) {
					json(0, '正在出售中!');
				}

			}else{

				$userPool = model('admin.scheduling.Scheduling')->UserPoolInof(['user_id' => $userInfo->id, 'statu' => 1]);
				if (count($userPool) < 1) {
					json(0, '未在订单池抢到额度，无法发布!');
				}
				if ($ooderInfo->sellType == 2 && $ooderInfo->status == 1) {
					json(0, '正在挂单中!');
				}
			}

			//验证是否已经发布过订单
//			if ($this->model->userOrdercheck("user_id='$userInfo->id' and  status<> 3 ")) {
//				json(2, '有在进行的订单！', '../deal.html');
//			}
			$data = array(
				'user_id' => $userInfo->id,
				'sid' => '',
				'unit_price' => $unit_price,
				'total' => $total,
				'Hangmoney' => $number * $unit_price,
				'sxf' => $sxf,
				'status' => 1,
				'Moneystatus' => 0,
				'pattern' => $patternId,
				'bank_id' => $bankId != '' ? $bankId : '',
				'alipay' => $alipay != '' ? $alipay : '',
				'alipay_pic' => '',
				'add_time' => get_datetime(),
				'order_time' => '',
				'payment_time' => '',
				'Moneystatus' => 0,
				'payBank_id' => '',
				'sellType' => $sellType,
			);
			//新增冻结积分
			$frostData = array(
				'user_id' => $userInfo->id,
				'frost_type' => '1',
				'money' => $total,
				'frost_time' => get_datetime(),
				'is_frost' => '2',
				'thawing_time' => 0,
			);
			$newMoney = $userInfo->up_integral -$total; //冻结后 会员所剩金额
			try {
				if (!!$this->model->AdduserOrder($data)) {
					if($data['sellType']==1){
					 model('admin.member.Member')->Addfrost($frostData);
					 model('admin.member.Member')->modMemberList($userInfo->id, ['up_integral' => $newMoney]);
					}
					$this->model->commit();
					json(2, '发布成功！', '../deal.html');

				}
			} catch (Exception $e) {
				$this->model->begin();
				json(0, error());
			}

		}

		//订单列表
		public function OrderList()
		{
			$this->Check('', '../index.html');
			$userInfo = $this->Memcache(2, session('uid'));//当前登录的会员
			if (!!$OrderList = $this->model->OrderList("status='1'and user_id <>'$userInfo->id' and sid='0'")) {
				json(1, $OrderList);
			}
		}

		/**
		 * 会员卖出
		 */
		public function sell()
		{
			$this->Check('', '../index.html');
			$oid = request('oid', 'num', true, '订单ID');//订单ID
			$OrderList = $this->model->userOrdercheck(['id' => $oid, 'status' => 1]);//发布的会员信息
			$Memcache = $this->Memcache(2, session('uid'));//当前登录的会员
			$userInfo = model('admin.member.Member')->checkMember(['id' => $Memcache->id], 'up_integral,id');

			//验证订单状态
			if ($this->model->userOrdercheck(['user_id' => $Memcache->id, 'status' => 2, 'Moneystatus' => 2])) {
				json(0, '有未完成的订单');
			}
			if (!$OrderList) {
				json(0, '当前订单可能已经正在交易');
			}
			//验证是否是自己的
			if ($userInfo->id == $OrderList->user_id) {
				json(0, '不能卖出当前');
			}
			if ($OrderList->sellType == 1) {
				if ($userInfo->up_integral < $OrderList->total) {
					json(0, '资金不足,无法出售！');
				}
				$this->Memcache(1, 'Order_id', $OrderList->id);
				json(2, '开始下单', 'Addsell.html');
			} else {
				$this->Memcache(1, 'buyOrder_id', $OrderList->id);
				json(2, '开始下单', 'buyAddsell.html');
			}


		}

		/**
		 * 查询当前出售订单信息
		 */
		public function sellLIst()
		{
			$this->Check('', '../index.html');
			$Order_id = $this->Memcache(2, 'Order_id');

			$userInfo = $this->Memcache(2, session('uid'));
			$Order = $this->model->userOrdercheck(['id' => $Order_id, 'status' => 1, 'Moneystatus' => 0]);

			if (!$Order) {
				//用户退出支付 或者或者非正常逻辑进入支付页面
//            $this->Memcache(3);
//            json(2, '非法请求', '../index.html');
			} else {
				$userbank = $this->model->UserbankInfo(['id' => $Order->bank_id, 'user_id' => $Order->user_id]);
				$data = array();
				$data['account_name'] = $userbank->account_name;
				$data['Card'] = $userbank->Card;
				$data['Bank_deposit'] = $userbank->Bank_deposit;
				$data['Hangmoney'] = $Order->Hangmoney;
				$data['unit_price'] = $Order->unit_price;
				$data['total'] = $Order->total;
				$data['sxf'] = $Order->sxf;
				$data['pattern'] = $Order->pattern;
				$data['alipay_pic'] = $Order->alipay_pic;
				$data['alipay'] = $Order->alipay;
				$data['add_time'] = $Order->add_time;
				$data['sellType'] = $Order->sellType;
				json(1, $data);
			}

		}

		public function buysellLIst()
		{
			$this->Check('', '../index.html');
			$Order_id = $this->Memcache(2, 'buyOrder_id');


			$Order = $this->model->userOrdercheck(['id' => $Order_id, 'status' => 1, 'Moneystatus' => 0]);

			if (!$Order) {
				//用户退出支付 或者或者非正常逻辑进入支付页面
//            $this->Memcache(3);
//            json(2, '非法请求', '../index.html');
			} else {
				$userbank = $this->model->UserbankInfo(['id' => $Order->bank_id, 'user_id' => $Order->user_id]);
				$data = array();
				$data['account_name'] = $userbank->account_name;
				$data['Card'] = $userbank->Card;
				$data['Bank_deposit'] = $userbank->Bank_deposit;
				$data['Hangmoney'] = $Order->Hangmoney;
				$data['unit_price'] = $Order->unit_price;
				$data['total'] = $Order->total;
				$data['sxf'] = $Order->sxf;
				$data['pattern'] = $Order->pattern;
				$data['alipay_pic'] = $Order->alipay_pic;
				$data['alipay'] = $Order->alipay;
				$data['add_time'] = $Order->add_time;
				$data['sellType'] = $Order->sellType;
				json(1, $data);
			}
		}

		public function Paylist()
		{
			$this->Check('', '../index.html');
			$Order_id = $this->Memcache(2, 'Order_id');

			$userInfo = $this->Memcache(2, session('uid'));
			$Order = $this->model->userOrdercheck(['id' => $Order_id, 'status' => 2, 'Moneystatus' => 2]);

			if (!$Order) {
				//用户退出支付 或者或者非正常逻辑进入支付页面
				$this->Memcache(3);
				json(2, '非法请求', '../index.html');
			} else {
				$userbank = $this->model->UserbankInfo(['id' => $Order->bank_id, 'user_id' => $Order->user_id]);
				$data = array();
				$data['account_name'] = $userbank->account_name;
				$data['Card'] = $userbank->Card;
				$data['Bank_deposit'] = $userbank->Bank_deposit;
				$data['Hangmoney'] = $Order->Hangmoney;
				$data['unit_price'] = $Order->unit_price;
				$data['total'] = $Order->total;
				$data['sxf'] = $Order->sxf;
				$data['pattern'] = $Order->pattern;
				$data['alipay_pic'] = $Order->alipay_pic;
				$data['alipay'] = $Order->alipay;
				$data['order_time'] = $Order->order_time;
				$data['shroff_number'] = $Order->shroff_number;
				$data['alipay_pic'] = $Order->alipay_pic;
				$data['id'] = $Order->id;
				$data['bid'] = $Order->payBank_id;
				json(1, $data);
			}

		}

		//确认订单
		public function modOrder()
		{
			$this->Check('', '../index.html');
			$patternId = request('patternId', 'num', true, '支付类型');//支付类型
			$password = request('password', 'is_null', true, '交易密码');//交易密码
			$shroff_number = request('shroff_number', 'is_null', true, '支付账号');//交易密码
			request('consent', 'is_null', true, '特别说明');//特别说明
			$Order_id = $this->Memcache(2, 'Order_id');
			$Memcache = $this->Memcache(2, session('uid'));
			$userInfo = model('admin.member.Member')->checkMember(['id' => $Memcache->id], 'up_integral');//当前登录会员的信息

//        if (md5(md5($password)) != $userInfo->TradersPassword) {
//            json(0, '交易密码错误!');
//        }
			if ($patternId == 1) {
				$file = request('file', 'url', true, '收款截图');//收款截图
			} else {
				$bankId = request('bankId', 'is_null', true, '银行卡ID');//银行卡ID
			}

			//检测是否重复支付
			$Order = $this->model->userOrdercheck(['id' => $Order_id, 'status' => 1, 'Moneystatus' => 0]);

			if (!$Order) {
				json(2, '当前订单已经在交易中', 'sell.html');
			} else {
				try {
					//要修改的订单数据
					$OrderData = array(
						'sid' => $Memcache->id,
						'status' => 2,
						'Moneystatus' => 2,
						'order_time' => get_datetime(),
						'alipay_pic' => $file, //付款二维码
						'patterntype' => $patternId,
						'shroff_number' => $shroff_number,
						'payBank_id' => $bankId,

					);

					if($Order->sellType==2){
						//新增冻结积分
						$frostData = array(
							'user_id' => $Memcache->id,
							'frost_type' => '1',
							'money' => $Order->total,
							'frost_time' => get_datetime(),
							'is_frost' => '2',
							'thawing_time' => 0,
						);
						$frostAdd = model('admin.member.Member')->Addfrost($frostData);
						$userMod = model('admin.member.Member')->modMemberList($Memcache->id, ['up_integral' => $newMoney]);
					}
					$newMoney = $userInfo->up_integral - $Order->total; //冻结后 会员所剩金额
					$OrderMod = $this->model->modUserOrder(['id' => $Order->id, 'user_id' => $Order->user_id, 'status' => 1], $OrderData);


					if (!!$OrderMod) {
						json(2, '等待卖家付款', 'selllist.html');
						$this->model->commit();
					}
				} catch (Exception $e) {
					$this->model->begin();
					json(0, error());
				}
			}

		}

		//当前会员发布的订单
		public function OrderLise()
		{
			$this->Check('', '../index.html');
			$Memcache = $this->Memcache(2, session('uid'));
			$OrderList = $this->model->UserOrderList(['user_id' => $Memcache->id, 'sellType' => 2]);
			if ($OrderList) {
				json(1, $OrderList);
			}
		}

		//当前卖出的订单
		public function UserOrderLise()
		{
			$this->Check('', '../index.html');
			$Memcache = $this->Memcache(2, session('uid'));

			$OrderList = $this->model->UserOrderList(['user_id' => $Memcache->id, 'sellType' => 1]);
			json(1, $OrderList);
			if ($OrderList) {
				json(1, $Memcache);
			}
		}

		//前往支付订单
		public function GetPay()
		{
			$this->Check('', '../index.html');
			$Oid = request('Oid', 'num', true, '订单ID');//订单ID

			$Memcache = $this->Memcache(2, session('uid'));
			$Order = $this->model->userOrdercheck(['id' => $Oid, 'user_id' => $Memcache->id, 'status' => 2, 'Moneystatus' => 2]);
			if (!$Order) {
				//验证订单ID是否存在 如果存在 直接跳转支付页面 不存在 重新写入Memcache
				json(0, '已经支付订单,请勿重复操作！');

			} else {
				$this->Memcache(1, 'Order_id', $Order->id);
				json(2, '前往支付', 'pay.html');

			}

		}

		//会员已经支付
		public function pay()
		{

			$this->Check('', '../index.html');
			$Oid = request('OrderId', 'num', true, '订单ID');//订单ID
			$file = request('file', 'url', true, '打款截图');//打款截图
			$patternId = request('patternId', 'null', true, '支付类型ID');//支付ID

			$Memcache = $this->Memcache(2, session('uid'));

			$Order = $this->model->userOrdercheck(['id' => $Oid, 'user_id' => $Memcache->id, 'status' => 2, 'Moneystatus' => 2,]);

			if ($Order) {
				$data = array(
					'remits_pic' => $file,
					'payment_time' => get_datetime(),
					'Moneystatus' => 1,
				);
				try {
					//要修改的订单数据
					$OrderMod = $this->model->modUserOrder(['id' => $Oid, 'user_id' => $Memcache->id, 'status' => 2, 'Moneystatus' => 2,], $data);
					if (!!$OrderMod) {
						json(2, '等待卖家确认', 'myOrder.html');
						$this->model->commit();
					}
				} catch (Exception $e) {
					$this->model->begin();
					json(0, error());
				}
			} else {
				json(0, '支付失败');
			}
		}

		public function affirmUrl()
		{
			$Oid = request('Oid', 'num', true, '订单ID');//订单ID
			$Memcache = $this->Memcache(2, session('uid'));

			$Order = $this->model->userOrdercheck(['id' => $Oid, 'sid' => $Memcache->id]);
			$frostInfo = model('admin.member.Member')->frostInfo(['user_id' => $Memcache->id, 'is_frost' => 2, 'frost_type' => 1]);
			$userInfo = model('admin.member.Member')->checkMember(['id' => $Order->user_id], 'up_integral,id');//发布订单会员的信息

			if ($Order) {
				//验证订单状态
				switch ($Order->status) {
					case 1:
						json(0, '操作失败！');
						break;
					case 2:
						$OrderData = array(
							'agreed_time' => get_datetime(),
							'status' => 3,
						);
						$frostData = array(
							'thawing_time' => get_datetime(),
							'is_frost' => 3,
						);
						$userDta = array(
							'up_integral' => $userInfo->up_integral + $frostInfo->money,
						);
						try {
							$frostMod = model('admin.member.Member')->Modfrost(['user_id' => $Memcache->id], $frostData);
							$OrderMod = $this->model->modUserOrder(['id' => $Oid], $OrderData);
							$userMod = model('admin.member.Member')->modMemberList($Order->user_id, $userDta);
							if (!!$frostMod and !!$OrderMod and !!$userMod) {
								json(1, '交易成功！');
							}
						} catch (Exception $e) {
							$this->model->begin();
							json(0, error());
						}

						break;
					case 3:
						json(0, '订单已经交易完成！');
						break;
					case 4:
						json(0, '订单已经取消！');
						break;
					default:
				}
			} else {
				json(0, '操作失败,订单不存在！');
			}

		}

		//查询卖家付款账号信息
		public function UserbankList()
		{
			$bid = request('bid', 'num', true, '卖家银行卡信息');//订单ID

			$field = $this->model->UserbankInfo(['id' => $bid]);
			json(1, $field);
		}
	}